#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>

using namespace std;

int T, n;
int group[200000], k;

struct COMMAND {
    int n1, n2, r;
    COMMAND() : n1(0), n2(0), r(0) {}
    COMMAND(int a, int b, int c) : n1(a), n2(b), r(c) {}
};
COMMAND cmd[100100];

int tab[200200], tot = 0;
int h[200200], u = 0;

int getInd(int x) {
    int f = 0, t = u - 1, m = (f + t) / 2;
    while (f < t) {
        m = (f + t) / 2;
        if (h[m] == x)
            return m;
        else if (h[m] < x)
            f = m + 1;
        else
            t = m - 1;
    }
    if (h[f] == x)
        return f;
    else if (h[t] == x)
        return t;
    cout << "NO FOUND\n";
    return -1;
}

int find(int x) {
    if (group[x] == x)
        return x;
    else
        return group[x] = find(group[x]);
}

void merge(int a, int b) {
    group[a] = b;
}

int main() {
    cin >> T;
    while (T--) {
        tot = u = 0;
        memset(group, 0, sizeof(group));
        memset(tab, 0, sizeof(tab));
        memset(h, 0, sizeof(h));
        cin >> n;
        for (int i = 0; i < n; i++)
            cin >> cmd[i].n1 >> cmd[i].n2 >> cmd[i].r;
        sort(cmd, cmd + n, [](COMMAND c1, COMMAND c2) { return c1.r > c2.r; });

        for (int i = 0; i < n; i++) {
            if (!cmd[i].r) break;
            tab[tot++] = cmd[i].n1;
            tab[tot++] = cmd[i].n2;
        }
        sort(tab, tab + tot);
        for (int i = 0; i < tot;) {
            h[++u] = tab[i];
            i++;
            while (i < tot && tab[i] == tab[i - 1])
                i++;
        }

        for (int i = 0; i <= u; i++)
            group[i] = i;

        bool flag = true;
        for (int i = 0; i < n; i++) {
            if (cmd[i].r) {
                merge(find(getInd(cmd[i].n1)), find(getInd(cmd[i].n2)));
            } else {
                if (find(getInd(cmd[i].n1)) == find(getInd(cmd[i].n2))) flag = false;
            }
        }
        cout << (flag ? "YES\n" : "NO\n");
    }
}